package com.hj.security.handler;

import com.hj.security.domain.resp.SecurityResp;
import com.hj.security.utils.HttpContextUtils;
import com.hj.security.utils.ResponseUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;


/**
 * @author hj
 * @Date 2023/4/26
 * @Description: json AuthenticationEntryPoint
 */
@Component
public class HjEntryPointHandler implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) {
        response.setContentType("application/json; charset=utf-8");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Origin", HttpContextUtils.getOrigin());
        SecurityResp resp = new SecurityResp();
        resp.setCode(401);
        resp.setMsg("请求错误或认证已过期,请重新登录");
        ResponseUtil.responseJson(response, resp);
    }


}
